package com.demo.pool;

import java.util.concurrent.*;
/**
 * new ThreadPoolExecutor.AbortPolicy() // 银行满了，还有人进来，不处理这个人的，抛出异
 常
 * new ThreadPoolExecutor.CallerRunsPolicy() // 哪来的去哪里！
 * new ThreadPoolExecutor.DiscardPolicy() //队列满了，丢掉任务，不会抛出异常！
 * new ThreadPoolExecutor.DiscardOldestPolicy() //队列满了，尝试去和最早的竞争，也不会
 抛出异常！
 */
public class Test {
    public static void main(String[] args) {
//        ExecutorService pool = Executors.newCachedThreadPool();
//        ExecutorService pool = Executors.newCachedThreadPool();
//        ExecutorService pool = Executors.newSingleThreadExecutor();
        //银行排队
        ExecutorService pool = new ThreadPoolExecutor(
                2,      //核心线程数
                5,  //最大并发数
                3,      //超时等待
                TimeUnit.SECONDS,       //等待单位
                /**
                 * 阻塞队列 有 四组API ，抛出异常和不抛出异常，阻塞和超时等待
                 */
                new LinkedBlockingDeque<>(3), //候选阻塞队列
                Executors.defaultThreadFactory(),//线程池工厂
                new ThreadPoolExecutor.DiscardOldestPolicy() //拒绝策略，尝试和最早的竞争，不会抛出异常
        );
        // 最大承载 阻塞 + MAX
        for (int i = 1; i <= 8; i++) {
            pool.execute(()->{
                System.out.println(Thread.currentThread().getName()+" ok");
            });
        }
        pool.shutdown();
    }
}
